package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.dao.WQbdFalSuperviseDao;
import com.ccp.dev.qbdms.model.WQbdFalSupervise;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 型号质量问题督办 信息操作服务类
 *
 * @author zhl
 * @date 2020-05-07 13:41:47
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdFalSuperviseService extends BaseService<WQbdFalSupervise> {

    @Resource
    private WQbdFalSuperviseDao dao;
    @Resource
    private ScriptImpl script;


    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<WQbdFalSupervise> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdFalSupervise data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    public List<Map> getOrg(Map map){
        return dao.getOrg(map);
    }

    public List<Map> getOrg2(Map map){
        return dao.getOrg2(map);
    }

    public List<WQbdFalSupervise> getByIds(String[] lAryId) {
        return dao.getByIds(lAryId);
    }

    public void suUpdate(Map map) {
        dao.suUpdate(map);
    }


    /**
     * 根据orgId 查其租户id
     * @param code 组织id
     * @return 租户id
     */
    public String getTenantIdByCode(String code) {
        String tenantId = "";
        //如果是租户 则查询关系表获取租户ID
        tenantId =  dao.getTenantIdByCode(code);
        return tenantId;
    }

    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WQbdFalSupervise getParentAndChilden(String id) {
        return getOneById(id);
    }

    /**
     * 保存接口传输的数据
     *
     * @param data 主表model,有子表的包含子表数据
     * @return
     */
    public int saveAll(List<WQbdFalSupervise> data,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(data,extendSysFileList);
        int i = 0;
        for (WQbdFalSupervise wQbdFalSupervise : data) {
            wQbdFalSupervise.setfIfsupervise("未下发");
            wQbdFalSupervise.setBelongdeptname(BaseUtil.getTenOrgCodePath(wQbdFalSupervise.getfDutydeptId()));
            // 下发后 清除 问题责任单位 -- 如果当前单位是三级单位，就不需要清空数据
            // 获取当前系统级别
            BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel(wQbdFalSupervise.getfDutydeptId());
            if(!"PLANTLEVEL".equals(currentSystemLevel.toString())){
                wQbdFalSupervise.setfDutydept("");
                wQbdFalSupervise.setfDutydeptId("");
            }
            if(BeanUtils.isEmpty(dao.getById(wQbdFalSupervise.getId()))){
                i += dao.add(wQbdFalSupervise);
            } else {
                i += dao.update(wQbdFalSupervise);
            }
        }
        return i;
    }

}
